/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graph;

import java.awt.Color;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 *
 * @author borysfan
 */
public class DijkstraResult{
    public static int fieldNum=5;
    private ArrayList<Node> nodeList;
    private double avgSpeed=0;
    private double dist=0;
    private double time=0;

    public DijkstraResult(){
        nodeList=new ArrayList<Node>();
    }

    public double roundD(double round){
        round=round*100;
        round=Math.floor(round);
        round=round/100;
        return round;
    }
    public int getCityNum(){
        return nodeList.size();
    }

    public double getAvgSpeed(){
        return roundD(avgSpeed);
    }

    public double getDistance(){
        return roundD(dist);
    }

    public double getTime(){

        return roundD(time);
    }

    public void addNode(Node n){
        nodeList.add(n);
        computeDistance();
        computeTime();
        avgSpeed=dist/time;
    }

    public ArrayList<Node> getNodeList(){
        return nodeList;
    }

    public void changeColor(Color c, int n){
        changeColor(Color.black);
        Graph graph = Graph.getInstance();
        for(int i=1;i<nodeList.size() && i< n;i++){
            Node a = nodeList.get(i-1);
            Node b = nodeList.get(i);
            Edge e = graph.getEdge(a, b);

            if(e!=null){
                System.out.println(e.getName());
                e.setColor(c);
                e.setDefColor(Color.green);
            }
        }
    }
    
    public void changeColor(Color c){
        for(int i=0;i<nodeList.size();i++){
            Node n = nodeList.get(i);
            n.setColor(c);
            n.setDefColor(Color.green);
        }
        Graph graph = Graph.getInstance();
        for(int i=1;i<nodeList.size();i++){
            Node a = nodeList.get(i-1);
            Node b = nodeList.get(i);
            Edge e = graph.getEdge(a, b);
            
            if(e!=null){
                System.out.println(e.getName());
                e.setColor(c);
                e.setDefColor(Color.green);
            }
        }

    }

    public Node get(int index){
        return nodeList.get(index);
    }

    @Override
    public boolean equals(Object o){
        Graph g = Graph.getInstance();
        if(o instanceof DijkstraResult){
            DijkstraResult dr = (DijkstraResult)o;
            if(dr.getCityNum()!=getCityNum()) return false;
            for(int i=0;i<dr.getCityNum()-1;i++){
                Edge e1=g.getEdge(dr.get(i), dr.get(i+1));
                Edge e2=g.getEdge(get(i), get(i+1));
                if(e1!=null && e2!=null){
                    if(e1.getId()!=e2.getId()) return false;
                }

            }
            return true;
        }else{
            return false;
        }

    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.nodeList != null ? this.nodeList.hashCode() : 0);
        return hash;
    }

    private void computeDistance(){
        double tmp=0;
        if(nodeList.size()>0){
            tmp = nodeList.get(nodeList.size()-1).getDistance();
        }
        dist=tmp;
    }

    private void computeTime() {
        double tmp=0;
        if(nodeList.size()>0){
            tmp = nodeList.get(nodeList.size()-1).getAvgTime();
        }
        time=tmp;
    }

   




   

}
